package com.bootdo.collection.service.impl;

import com.bootdo.collection.dao.SourceStationDao;
import com.bootdo.collection.domain.SourceStationDO;
import com.bootdo.collection.service.SourceStationService;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SourceStationServiceImpl implements SourceStationService {
    @Autowired
    private SourceStationDao sourceStationDao;
    @Override
    public List<SourceStationDO> list(Map<String,Object> params) {
        List<SourceStationDO> list = sourceStationDao.list(params);
        return list;
    }

    @Override
    public SourceStationDO get(int id) {
        SourceStationDO stationDO = sourceStationDao.get(id);
        return stationDO;
    }

    @Override
    public int remove(int id) {
        return 0;
    }

    @Override
    public int save(SourceStationDO sourceStationDO) {
        //如果stationId == 0,则证明是添加站点，否则就是添加栏目
       sourceStationDO.setType(1);
       return sourceStationDao.addStation(sourceStationDO);

    }


    @Override
    public int edit(SourceStationDO sourceStationDO) {
        return 0;
    }

    @Override
    public int update(SourceStationDO sourceStationDO) {
        int s = sourceStationDao.update(sourceStationDO);
        return s;
    }

    @Override
    public Tree<SourceStationDO> getTree() {
        List<Tree<SourceStationDO>> treeList = new ArrayList<>();
        List<SourceStationDO> stationDOS = sourceStationDao.list(new HashMap<>());
        for(SourceStationDO stationDO : stationDOS){
            Tree<SourceStationDO> stationDOTree = new Tree<>();
            stationDOTree.setId(stationDO.getId().toString());
            stationDOTree.setText(stationDO.getName());
            stationDOTree.setParentId(stationDO.getParentId().toString());
            Map<String, Object> state = new HashMap<>();
            state.put("opened", false);
            stationDOTree.setState(state);
            treeList.add(stationDOTree);
        }
        Tree<SourceStationDO> tree = BuildTree.build(treeList);
        return tree;
    }

    @Override
    public int getTotal() {
        return sourceStationDao.getTotal();
    }

    @Override
    public List<String> getSubName(int id) {
//        StringBuilder stringBuilder = new StringBuilder();
//        List<Integer> allSubId = getSubIds(parentId);
//        for (int id : allSubId) {
//            stringBuilder.append("," + sourceStationDao.get(id).getName());
//        }
//        stringBuilder.deleteCharAt(0);

        List<String> subName = sourceStationDao.getByRelation(id);
//        String[] subName = stringBuilder.toString().split(",");
        return subName;
    }

}
